# -*- coding: UTF-8 -*-
# version 1.0
# Ip网络
from mxsoftpy import View, Response
from index import _
from server.common.NetworkManage.IpAM.IpNetwork import IpNetworkServer


class RequestBase(View):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.request.POST['company'] = self.request.company


class IpNetworkAdd(RequestBase):

    def post(self):
        """
        添加配置
        """
        try:
            return _('新增成功'), {'id': IpNetworkServer().create(**self.request.POST)}
        except Exception as e:
            return str(e)


class IpNetworkList(RequestBase):

    def post(self):
        """
        获取列表
        """
        callback = self.request.callback
        if callback:
            data = '%s(%s)' % (callback, IpNetworkServer().list(**self.request.POST))
        else:
            data = IpNetworkServer().list(**self.request.POST)
        return Response(data)


class IpNetworkUpdate(RequestBase):

    def post(self):
        """
        更新IP网络名称
        """
        try:
            return _('修改成功'), IpNetworkServer().update(**self.request.POST)
        except Exception as e:
            return str(e)


class IpNetworkDelete(RequestBase):

    def post(self):
        """
        删除配置
        """
        return _('删除成功'), IpNetworkServer().delete(**self.request.POST)


class IpNetworkRetrieve(RequestBase):

    def post(self):
        """
        回显数据
        """
        return '', IpNetworkServer().retrieve(**self.request.POST)


class IpNetworkIgnore(RequestBase):

    def post(self):
        """
        忽略网络
        :return:
        """
        return _('忽略成功'), IpNetworkServer().ignore(**self.request.POST)


class IpGroupSet(RequestBase):

    def post(self):
        """
        新增网络组
        :return:
        """
        try:
            return _('新增成功'), {'id': IpNetworkServer().GroupSet(**self.request.POST)}
        except Exception as e:
            return str(e)


class IpGroupDelete(RequestBase):

    def post(self):
        """
        删除网络组
        :return:
        """
        return _('添加成功'), IpNetworkServer().GroupDelete(**self.request.POST)


class IpGroupIgnore(RequestBase):

    def post(self):
        """
        忽略网络组
        :return:
        """
        return _('忽略成功'), IpNetworkServer().GroupIgnore(**self.request.POST)


class IpShow(RequestBase):

    def post(self):
        """
        IP列表显示
        :return:
        """
        if self.request.POST['type'] == '1':
            callback = self.request.callback
            if callback:
                data = '%s(%s)' % (callback, IpNetworkServer().IpShow(**self.request.POST))
            else:
                data = IpNetworkServer().IpShow(**self.request.POST)
            return Response(data)
        return '', IpNetworkServer().IpShow(**self.request.POST)


class TsShow(RequestBase):

    def post(self):
        """
        TS列表显示
        :return:
        """
        return '', IpNetworkServer().TsShow(**self.request.POST)


class move(RequestBase):

    def post(self):
        """
        移动组或者子网
        :return:
        """
        try:
            return _('移动成功'), IpNetworkServer().move(**self.request.POST)
        except Exception as e:
            return str(e)


class IgnoreTree(RequestBase):

    def post(self):
        """
        忽略网络
        :return:
        """
        return '', IpNetworkServer().IgnoreTree(**self.request.POST)
